Learning Goals

At the end of this exercise, you will be able to:
1. Produce distribution maps in R.

Spatial Data in R

There are many packages and techniques for working with spatial data in R. We will cover just some of the basics. One nice package is ggmap, which allows us to get base maps from Google Maps, OpenStreetMap, and Stamen Maps. It also works well with ggplot2.

If you completed part 1 of today’s lab then you should have the following packages installed. If not, then you should get them going now. You need to do these in order!

devtools and ggmap

#install.packages("devtools")
#library(devtools)
#devtools::install_github("dkahle/ggmap")

albersusa

#install.packages("remotes")
#remotes::install_github("hrbrmstr/albersusa")

Load the libraries

library(tidyverse)
library(here)
library(janitor)
library(ggmap)
library(dplyr)

Let’s load our processed data from the first part of the lab.

spiders <- readr::read_csv("data/spiders_with_locs.csv") %>% clean_names()

There is an error in one of the coordinates that we will fix here.

spiders <- spiders %>% filter(latitude<=42)

Create Base Map

Our goal here is to plot the spiders locations from the columns which contain the latitude and longitude. First, we need to get a base map for plotting our points on. We could plot them without a base map, but that wouldn’t give us any context as to where they are in space. To get a base map we specify a min and max of each x and y coordinate, and create a bounding box.

We set the bounding box to a little outside our min and max locations with f = 0.05.

summary() gives us our min and max.

spiders %>% 
  select(latitude, longitude) %>% 
  summary()
##     latitude       longitude     
##  Min.   :34.67   Min.   :-124.1  
##  1st Qu.:37.88   1st Qu.:-122.5  
##  Median :38.19   Median :-122.1  
##  Mean   :38.47   Mean   :-121.6  
##  3rd Qu.:38.88   3rd Qu.:-120.5  
##  Max.   :41.80   Max.   :-115.5

Now we set the bounding box.

lat <- c(34.67, 41.80)
long <- c(-124.1, -115.5)
bbox <- make_bbox(long, lat, f = 0.05)

Let’s get a base map for our bounding box area. We will use the stamen maps because they are free. There are several different map types, including: terrain-labels, terrain-lines, toner, toner-2011, toner-background, toner-hybrid, toner-lines, toner-lite, and watercolor.

map1 <- get_map(bbox, maptype = "toner-hybrid", source = "stamen")
## Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under ODbL.
ggmap(map1)

map2 <- get_map(bbox, maptype = "watercolor", source = "stamen")
## Map tiles by Stamen Design, under CC BY 3.0. Data by OpenStreetMap, under CC BY SA.
ggmap(map2)

Adding Points to Base Map

ggmap works well with ggplot2. To add our points we only need to specify the x and y location similar to how we made charts in previous labs.

ggmap(map1) + 
  geom_point(data = spiders, aes(longitude, latitude)) +
  labs(x = "Longitude", y = "Latitude", title = "Spider Locations")

Practice

  1. Map the spider locations with a different type of base map. Try to adjust the map using different aesthetics including size, color, and alpha.

Albers USA

The Albers USA package is a very fast way to produce clean maps that include nice overlays of counties across the US.

library(albersusa)

State Boundaries.

us_comp <- usa_sf() # get a composite map of the USA

County boundaries in each state.

cnty_comp <- counties_sf() # get a composite map of USA counties

Map of the USA- notice that you can still use themes. geom_sf stands for simple feature.

ggplot() + 
  geom_sf(data = us_comp, size = 0.125) + 
  theme_linedraw()+
  labs(title = "USA State Boundaries")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Since we want to produce a map of our spiders we need to restrict the map to California.

ca_comp <- us_comp %>% 
  filter(name=="California")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
ca_cnty_comp <- cnty_comp %>% 
  filter(state=="California")
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Our base map

ggplot() +
  geom_sf(data = ca_comp, size = 0.125)+
  geom_sf(data = ca_cnty_comp, size = 0.125)

All we do now is add the geom_point() layer.

ggplot() +
  geom_sf(data = ca_comp, size = 0.125)+
  geom_sf(data = ca_cnty_comp, size = 0.125)+
  geom_point(data = spiders, aes(longitude, latitude))

We can spice things up a bit by adding aesthetics.

ggplot() +
  geom_sf(data = ca_comp, size = 0.125)+
  geom_sf(data = ca_cnty_comp, size = 0.125)+
  geom_point(data = spiders, aes(longitude, latitude), size=0.9, shape=15)+
  labs(x = "Figure 1: Distribution of Usofila spiders")+
  theme_minimal() +
  theme(axis.text = element_blank()) +
  theme(axis.title.x = element_text(hjust=0.5, size = 10)) +
  theme(axis.title.y = element_blank()) +
  theme(panel.grid = element_blank()) +
  theme(legend.position = c(0.9, 0.3)) -> uso_map
uso_map

Practice

  1. Make a new column that labels each specimen as occurring in a cave or on the surface. The word “Cave” is part of the locality information.

  2. Now adjust the plot above to give a different color for specimens that occur in caves.

That’s it, let’s take a break!

–>Home